﻿
// CGWORK19190111View.cpp: CCGWORK19190111View 类的实现
//

#include "pch.h"
#include "framework.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义，并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "CGWORK19190111.h"
#endif

#include "CGWORK19190111Doc.h"
#include "CGWORK19190111View.h"
#include"SetColor.h"
#include"DrawSquare.h"
#include"DrawCircle.h"
#include"DrawLine.h"
#include"DrawPolygon.h"
#include"DrawMode.h"
#include"DrawBezier.h"
#include"DrawCube.h"
#include"SetStep.h"
#include<typeinfo>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CCGWORK19190111View

IMPLEMENT_DYNCREATE(CCGWORK19190111View, CView)

BEGIN_MESSAGE_MAP(CCGWORK19190111View, CView)
	// 标准打印命令
	ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CView::OnFilePrintPreview)
	ON_COMMAND(ID_DRAW_SETCOLOR, &CCGWORK19190111View::OnDrawSetcolor)
	ON_COMMAND(ID_LINE, &CCGWORK19190111View::OnLine)
	ON_WM_LBUTTONDOWN()
	ON_WM_MOUSEMOVE()
	ON_COMMAND(ID_DRAW2D_DRAWCIRCLE, &CCGWORK19190111View::OnDraw2dDrawcircle)
	ON_COMMAND(ID_DRAW2D_DRAWLINE, &CCGWORK19190111View::OnDraw2dDrawline)
	ON_COMMAND(ID_DRAW2D_DRAWSQUARE, &CCGWORK19190111View::OnDraw2dDrawsquare)
	ON_WM_RBUTTONDOWN()
	ON_COMMAND(ID_DRAW2D_DRAWPOLY, &CCGWORK19190111View::OnDraw2dDrawpoly)
	ON_COMMAND(ID_DRAW2D_DRAWBEZIER, &CCGWORK19190111View::OnDraw2dDrawbezier)
	ON_COMMAND(ID_DRAW_SETFILLCOLOR, &CCGWORK19190111View::OnDrawSetfillcolor)
	ON_COMMAND(ID_DRAWCUBE, &CCGWORK19190111View::OnDrawcube)
	ON_WM_KEYDOWN()
	ON_COMMAND(ID_moveX, &CCGWORK19190111View::OnMovex)
	ON_COMMAND(ID_movey, &CCGWORK19190111View::OnMovey)
	ON_COMMAND(ID_MOVE_Z, &CCGWORK19190111View::OnMoveZ)
	ON_COMMAND(ID_MOVE_Y, &CCGWORK19190111View::OnMoveY)
	ON_COMMAND(ID_MOVE_X, &CCGWORK19190111View::OnMoveX)
	ON_COMMAND(ID_ROTATE_X, &CCGWORK19190111View::OnRotateX)
	ON_COMMAND(ID_ROTATE_Z, &CCGWORK19190111View::OnRotateZ)
	ON_COMMAND(ID_ROTATE_Y, &CCGWORK19190111View::OnRotateY)
	ON_COMMAND(ID_SETSTEPS, &CCGWORK19190111View::OnSetsteps)
	ON_COMMAND(ID_CLEARWINDOW, &CCGWORK19190111View::OnClearwindow)
END_MESSAGE_MAP()

// CCGWORK19190111View 构造/析构

CCGWORK19190111View::CCGWORK19190111View() noexcept
{
	// TODO: 在此处添加构造代码
	r = 0;
	g = 0;
	b = 0;
	lastx = 0;
	lasty = 0;
	haslast = false;
	currMode = new DrawSquare();
	dragX = 0;
	dragY = 0;
	fill_r = 0;
	fill_g = 0;
	fill_b = 0;
	move_step = 10;
	rotate_angle = 90;
	center = DrawCube::XYZ::X;
}

CCGWORK19190111View::~CCGWORK19190111View()
{
	delete currMode;
}

BOOL CCGWORK19190111View::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: 在此处通过修改
	//  CREATESTRUCT cs 来修改窗口类或样式

	return CView::PreCreateWindow(cs);
}

// CCGWORK19190111View 绘图

void CCGWORK19190111View::OnDraw(CDC* /*pDC*/)
{
	CCGWORK19190111Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	// TODO: 在此处为本机数据添加绘制代码
}


// CCGWORK19190111View 打印

BOOL CCGWORK19190111View::OnPreparePrinting(CPrintInfo* pInfo)
{
	// 默认准备
	return DoPreparePrinting(pInfo);
}

void CCGWORK19190111View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 添加额外的打印前进行的初始化过程
}

void CCGWORK19190111View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 添加打印后进行的清理过程
}


// CCGWORK19190111View 诊断

#ifdef _DEBUG
void CCGWORK19190111View::AssertValid() const
{
	CView::AssertValid();
}

void CCGWORK19190111View::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CCGWORK19190111Doc* CCGWORK19190111View::GetDocument() const // 非调试版本是内联的
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCGWORK19190111Doc)));
	return (CCGWORK19190111Doc*)m_pDocument;
}
#endif //_DEBUG


// CCGWORK19190111View 消息处理程序


void CCGWORK19190111View::OnDrawSetcolor()
{
	SetColor dia;
	dia.m_b = b;
	dia.m_g = g;
	dia.m_r = r;
	if (dia.DoModal() == IDOK)
	{
		b=dia.m_b;
		g=dia.m_g;
		r=dia.m_r;
	}
}


void CCGWORK19190111View::OnLine()
{
	// TODO: 在此添加命令处理程序代码
}


void CCGWORK19190111View::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	currMode->update(this->GetDC(), point.x, point.y, currColor());
	CView::OnLButtonDown(nFlags, point);
}


COLORREF CCGWORK19190111View::currColor()
{
	// TODO: 在此处添加实现代码.
	return RGB(r,g,b);
}


void CCGWORK19190111View::OnMouseMove(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if (!currMode->isOver())
	{
		CDC* pDC = this->GetDC();//构造设备环境对象
		currMode->drag(pDC, dragX, dragY, point.x, point.y, currColor());
	}
	dragX = point.x;
	dragY = point.y;
	CView::OnMouseMove(nFlags, point);
}


void CCGWORK19190111View::OnDraw2dDrawcircle()
{
	delete this->currMode;
	currMode = new DrawCircle();
}


void CCGWORK19190111View::OnDraw2dDrawline()
{
	delete this->currMode;
	currMode = new DrawLine();
}


void CCGWORK19190111View::OnDraw2dDrawsquare()
{
	delete this->currMode;
	currMode = new DrawSquare();
}


void CCGWORK19190111View::OnRButtonDown(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	DrawPolygon tmp;
	DrawPolygon* p;
	DrawLine line;
	auto pDC = this->GetDC();
	if (typeid(*this->currMode) == typeid(tmp))
	{
		p=(DrawPolygon*)this->currMode;
		pDC->SetROP2(R2_NOTXORPEN);
		line.draw(pDC, p->lastx.back(), p->lasty.back(), point.x, point.y, currColor());
		p->finish(pDC,fillColor());
	}
	CView::OnRButtonDown(nFlags, point);
}


void CCGWORK19190111View::OnDraw2dDrawpoly()
{
	delete this->currMode;
	currMode = new DrawPolygon();
}


void CCGWORK19190111View::resetPointCnt()
{
	this->haslast = 1;
}


void CCGWORK19190111View::OnDraw2dDrawbezier()
{
	delete this->currMode;
	currMode = new DrawBezier();
	//Invalidate(true);
}


void CCGWORK19190111View::OnDrawSetfillcolor()
{
	SetColor dia;
	dia.m_b = fill_b;
	dia.m_g = fill_g;
	dia.m_r = fill_r;
	if (dia.DoModal() == IDOK)
	{
		fill_b = dia.m_b;
		fill_g = dia.m_g;
		fill_r = dia.m_r;
	}
}


void CCGWORK19190111View::OnDrawcube()
{
	//Invalidate(true);
	delete this->currMode;
	this->currMode = new DrawCube();
	auto p = (DrawCube*)currMode;
	p->draw(this->GetDC());
}


void CCGWORK19190111View::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if (typeid(*currMode) == typeid(DrawCube))
	{
		auto p = (DrawCube*)currMode;
		if (p->MODE_NOW == DrawCube::MODE::MOVE)
		{
			if (nFlags == 30)
			{
				p->move(this->GetDC(), this->center, move_step);

			}
			else if (nFlags == 38)
			{
				p->move(this->GetDC(), this->center, move_step*-1);

			}
		}
		else
		{
			if (nFlags == 30)
			{
				p->rotate(this->GetDC(), this->center, rotate_angle);

			}
			else if (nFlags == 38)
			{
				p->rotate(this->GetDC(), this->center, rotate_angle*-1);

			}
		}
	}
	CView::OnKeyDown(nChar, nRepCnt, nFlags);
}


void CCGWORK19190111View::OnMovex()
{
	this->center = DrawCube::X;
}


void CCGWORK19190111View::OnMovey()
{
	this->center = DrawCube::X;
}


void CCGWORK19190111View::OnMoveZ()
{
	if (typeid(*currMode) == typeid(DrawCube))
	{
		this->center = DrawCube::Z;
		auto p = (DrawCube*)currMode;
		p->setMode(DrawCube::MODE::MOVE);
	}
}


void CCGWORK19190111View::OnMoveY()
{
	if (typeid(*currMode) == typeid(DrawCube))
	{
		this->center = DrawCube::Y;
		auto p = (DrawCube*)currMode;
		p->setMode(DrawCube::MODE::MOVE);
	}
}


void CCGWORK19190111View::OnMoveX()
{
	if (typeid(*currMode) == typeid(DrawCube))
	{
		this->center = DrawCube::X;
		auto p = (DrawCube*)currMode;
		p->setMode(DrawCube::MODE::MOVE);
	}
}


void CCGWORK19190111View::OnRotateX()
{
	if (typeid(*currMode) == typeid(DrawCube))
	{
		this->center = DrawCube::X;
		auto p = (DrawCube*)currMode;
		p->setMode(DrawCube::MODE::ROTATE);
	}
}


void CCGWORK19190111View::OnRotateZ()
{
	if (typeid(*currMode) == typeid(DrawCube))
	{
		this->center = DrawCube::Z;
		auto p = (DrawCube*)currMode;
		p->setMode(DrawCube::MODE::ROTATE);
	}
}


void CCGWORK19190111View::OnRotateY()
{
	if (typeid(*currMode) == typeid(DrawCube))
	{
		this->center = DrawCube::Y;
		auto p = (DrawCube*)currMode;
		p->setMode(DrawCube::MODE::ROTATE);
	}
}


void CCGWORK19190111View::OnSetsteps()
{
	SetStep dia;
	dia.angle = this->rotate_angle;
	dia.step = this->move_step;
	if (dia.DoModal() == IDOK)
	{
		this->rotate_angle=dia.angle;
		this->move_step=dia.step;
	}
}


void CCGWORK19190111View::OnClearwindow()
{
	Invalidate(true);
}
